﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyFloridaHouse.libSTO;
using MyFloridaHouse.DataAccess;
using MyFloridaHouse.LibDTO;
using Microsoft.SqlServer.Types;
using System.Data.SqlTypes;
using MyFloridaHouse.libSvDTO;

namespace MyFloridaHouse.libServiceBusinessRules
{
    /// <summary>
    /// This class is used get the regions that are in view 
    /// based on a bounding box
    /// TODO: research why filter artifact return empty polygon sometimes...
    /// </summary>
    public static class BoundingBoxRegions
    {
        public static LayerRegionsShapesSTO GetLayerRegionsShapesInView(double iZoomLevel, int iTargetLayerID, double iNorth, double iEast, double iSouth, double iWest)
        {
            LayerRegionsShapesSTO results = new LayerRegionsShapesSTO();
            results.layerID = iTargetLayerID;
            int zoomLevel = (int)iZoomLevel;
            try
            {
                DAL_CensusData dal = new DAL_CensusData();
                var queryResults = dal.GetRegionsShapesInView(iZoomLevel, iTargetLayerID, iNorth, iEast, iSouth, iWest);
                var x = from q in queryResults
                        select q;
                List<RegionShapeDTO> regionShapeList = x.ToList();

                foreach (RegionShapeDTO geom in regionShapeList)
                {
                    try
                    {
                        if ((geom.mapGeom.STGeometryType() == "POLYGON") || (geom.mapGeom.STGeometryType() == "MULTIPOLYGON"))
                        {
                            List<Polygon> polygons = PolygonTools.GetPolygons(geom.mapGeom, zoomLevel);
                            List<Polygon> polygonsCleaned = PolygonTools.CleanPolygons(polygons);
                            SqlGeometry newGeom = SQLGeometryTools.CreateNewGeomFromReducedShape(polygonsCleaned);
                            geom.mapGeom = newGeom;
                        }
                    }
                    catch (Exception ex)
                    {
                        string test = ex.ToString();
                    }
                }


                List<RegionDTO> returnRegionsList =
                    regionShapeList.ConvertAll(newList => new RegionDTO
                    {
                        regionID = newList.regionID,
                        centerLatY = newList.centerLatY,
                        centerLngX = newList.centerLngX,
                        boundingBox = BoundingBoxHelpers.GeometrySTEnvelopeToBox(newList.mapGeom.MakeValid().STEnvelope()),
                        mapObject = newList.mapGeom.STAsText().ToSqlString().ToString()
                    });


                results.regionList = returnRegionsList;

            }
            catch (Exception ex)
            {
                results.errorMessage = ex.Message;
                return results;
            }
            return results;
        }


        public static LayerRegionsDataSTO GetLayerRegionsDataInView(int iTargetLayerID, string iRegionIDList, string iSourceLayerIDList, string iDictionaryIDList)
        {
            LayerRegionsDataSTO results = new LayerRegionsDataSTO();
            try
            {
                DAL_CensusData dal = new DAL_CensusData();
                var queryResults2 = dal.getRegionsDataInView(iTargetLayerID, iRegionIDList, iSourceLayerIDList, iDictionaryIDList);
                var z = from q in queryResults2
                        select q;
                results.regionsDataList = z.ToList();
                results.layerID = iTargetLayerID;
                return results;
            }
            catch (Exception ex)
            {
                results.errorMessage = ex.Message;
                return results;
            }
        }



    }
}

